export default class LocalCache {
    static _gameName: string = "sdk_2d_combatGame_sgrdz";
    static _instance: LocalCache = null;
    private data: object = {
        BGMSwitch: true,//bgm开关
        EffectSwitch: true,//音效开关
        vibrationSwitch: true,//振动开关
        coin: 0,//金币
    }
    constructor() {
        this.init();
    }
    static get instance(): LocalCache {
        return (LocalCache._instance ? LocalCache._instance : (LocalCache._instance = new LocalCache()));
    }
    init() {
        let data = cc.sys.localStorage.getItem(LocalCache._gameName);
        if (data == '') {
            data = null;
        }
        data = JSON.parse(data);
        //注意如果没有值的话返回的是undefined字符串
        if (data && data !== "undefined") {
            this.data = data;
            console.log(this.data);
        }
    }
    setItem(name, value: any) {
        this.data[name] = value;
        this.saveAllCacheData();
    }
    getItem(name) {
        return this.data[name];
    }

    removeAllItem() {
        cc.sys.localStorage.removeItem(LocalCache._gameName);
    }

    saveAllCacheData() {
        cc.sys.localStorage.setItem(LocalCache._gameName, JSON.stringify(this.data));
    }
}